package com.bootx.service.impl;

import com.bootx.dao.AiRoleChatMessageDao;
import com.bootx.dao.AiRoleDao;
import com.bootx.entity.AiRole;
import com.bootx.entity.AiRoleChatMessage;
import com.bootx.entity.Member;
import com.bootx.entity.MessageType;
import com.bootx.service.AiRoleChatMessageService;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.UserMessage;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author black
 */
@Service
public class AiRoleChatMessageServiceImpl extends BaseServiceImpl<AiRoleChatMessage,Long> implements AiRoleChatMessageService {

    @Resource
    private AiRoleChatMessageDao aiRoleChatMessageDao;

    @Override
    public List<ChatMessage> getHistory(Member member, AiRole aiRole) {

        List<AiRoleChatMessage> all = super.findAll();
        List<ChatMessage> messages = new ArrayList<>();
        all.forEach(aiRoleChatMessage -> {
           if(aiRoleChatMessage.getMessageType()== MessageType.USER){
               messages.add(UserMessage.userMessage(aiRoleChatMessage.getContent()));
           }else if(aiRoleChatMessage.getMessageType()== MessageType.ASSISTANT){
               messages.add(AiMessage.aiMessage(aiRoleChatMessage.getContent()));
           }
        });
        return messages;
    }

    @Override
    public List<AiRoleChatMessage> findList(Member member, AiRole aiRole) {
        return aiRoleChatMessageDao.findList(member,aiRole);
    }
}
